package com.xsz.service;

import com.xsz.entity.Info;
import com.xsz.entity.User;
import com.xsz.repository.InfoRepository;
import com.xsz.util.KeyUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service

public class InfoService {
	@Resource
    InfoRepository infoRepository;

	//新建
	public Info add(Info info, User user) {
		info.setId(KeyUtil.getId());
		info.setCreateby(user.getId());
		info.setCreatename(user.getName());
		Date t1=new Date();
		SimpleDateFormat sf=new SimpleDateFormat("yyyy/MM/dd-HH:mm");
		String dateString=sf.format(t1);
		info.setCreatetime(dateString);
		return infoRepository.save(info);
	}
	
	//修改

	public Info update(Info info, User user) {
		info.setCreateby(user.getId());
		info.setCreatename(user.getName());
		return infoRepository.save(info);
	}
	
	
	//删除

	public void delete(String id) {
		 infoRepository.deleteById(id);
	}
	
	//根据登录用户id查询
	public List<Info> selectById(String id) {
		 return infoRepository.findByCreateby(id);
	}

	//查询所有
	public List<Info> getAll() {
		return infoRepository.findAll();
	}

	//根据infoid查询
	public Optional<Info> selectByInfoId(String id){return infoRepository.findById(id);}

	//根据登录用户id查询
	public List<Info> selectByType() {
		return infoRepository.findByType(1);
	}

	//根据登录用户id查询
	public List<Info> selectByTitleAndProducttype(String title,String producttype) {
		return infoRepository.findByTileLikeAndProducttype("%"+title+"%",producttype);
	}
//	public Info login(String name, String pwd) {
//		Info info=infoRepository.findByNameAndPwd(name, pwd);
//		return info;
//	}
//
//	public List<Info> likeName(String name) {
//		return infoRepository.findByNameLike(name+"%");
//	}
//
//	public Info getByName(String name) {
//		return infoRepository.findByName(name);
//	}

}
